package com.gxa.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.common.R;
import com.gxa.common.Status;
import com.gxa.entity.SysMenu;
import com.gxa.entity.SysProviderProgress;
import com.gxa.entity.SysRole;
import com.gxa.entity.SysUser;
import com.gxa.mapper.RoleManagementMapper;
import com.gxa.mapper.SysMenuMapper;
import com.gxa.mapper.SysRoleMapper;
import com.gxa.mapper.SysSubMenuMapper;
import com.gxa.service.RoleManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleManagementServiceImpl implements RoleManagementService {

    @Autowired
    RoleManagementMapper roleManagementMapper;

    @Autowired
    SysRoleMapper sysRoleMapper;
    @Autowired
    SysMenuMapper sysMenuMapper;
    @Autowired
    SysSubMenuMapper sysSubMenuMapper;


    /**
     * 查询所有角色列表
     *
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public R findRoleList(Integer currentPage, Integer pageSize) {
        R r = new R();
        PageHelper.startPage(currentPage, pageSize);
        List roleList = roleManagementMapper.findRoleList();
        PageInfo<SysProviderProgress> pageInfo = new PageInfo(roleList);
        if(pageInfo != null){
            r.setCode(200);
            r.setMsg("success");
            r.setData(pageInfo);
        }else{
        r.setCode(500);
        r.setMsg("服务器开小差了");
    }
        return r;
    }

    /**
     * 新增角色
     * @param sysRole
     * @return
     */
    @Override
    public R addCRole(SysRole sysRole) {
        R r = new R();
        int i = roleManagementMapper.addRole(sysRole);
        if (i>0){
            r.setCode(200);
            r.setMsg("success");
        }else {
            r.setCode(500);
            r.setMsg("服务器开小差了");
        }
        return r;
    }

    /**
     * 删除角色信息
     * @param id
     * @return
     */
    @Override
    public R delRole(Integer id) {
        R r = new R();
        int i = roleManagementMapper.delRole(id);
        if (i>0){
            r.setCode(200);
            r.setMsg("success");
        }else{
            r.setCode(500);
            r.setMsg("服务器开小差了");
        }
        return r;
    }

    /**
     * 修改角色信息
     * @param sysRole
     * @return
     */
    @Override
    public R updateMenu(SysRole sysRole) {
        R r = new R();
        int i = roleManagementMapper.updateMenu(sysRole);
        if (i>0){
            r.setCode(200);
            r.setMsg("success");
        }else{
            r.setCode(500);
            r.setMsg("服务器开小差了");
        }
        return r;
    }

    @Override
    public R findRoleByName(String name, Integer currentPage, Integer pageSize) {
        R r = new R();
        PageHelper.startPage(currentPage,pageSize);
        List roleByName = roleManagementMapper.findRoleByName(name);
        PageInfo<SysProviderProgress> pageInfo = new PageInfo<>(roleByName);
        if (pageInfo != null) {
            r.setCode(200);
            r.setMsg("success");
            r.setData(pageInfo);
        } else {
            r.setCode(500);
            r.setMsg("服务器开小差了");
        }
        return r;
    }

    private List<SysMenu> getUserPermissionByRoleId(Integer id) {
        List<Integer> menuIdsByRoleId = sysRoleMapper.getMenuIdsByRoleId(id);
        //判断是否有权限
        if (menuIdsByRoleId.size() == 0) {
            return null;
        }
        List<SysMenu> sysMenuByRoleIds = sysMenuMapper.getSysMenuByRoleIds(menuIdsByRoleId);
        return sysMenuByRoleIds;
    }

    @Override
    public R findMenuListByRoleId(Integer id) {
        R result = new R();
        result.fromStatus(Status.SUCCESS);
        List<SysMenu> userPermissionByRoleId = getUserPermissionByRoleId(id);
        if (userPermissionByRoleId==null){
            return R.failure("没有任何权限信息！");
        }
        result.setData(userPermissionByRoleId);
        return result;
    }
}
